/*

*/
const {
    remote,
    webContents
} = require('electron')

//获取webcontents实例
function findCurrentWebContents() {
    return remote.getCurrentWebContents();
    let contents1 = win.webContents;
    let ctId = contents1.id;
    let contents2 = require('electron').WebContents.fromId(ctId);
    let contents3 = require('electron').WebContents.getFocusedWebContents();
}

//遍历页面内容
function browserAllObject(contents) {
    let webContentAttr = contents.getAllWebContents();
    for (const webContent of webContentAttr) {
        console.log(webContent);
        console.log(webContent.getURL());
    }
}

//页面加载事件和触发顺序
function webContentsEvent(webContent) {
    webContent.on('did-start-loading', (event, args) => {
        console.log('did-start-loading')
        console.log('页面加载的第一个事件')
    })
    webContent.on('page-title-updated', (event, args) => {
        console.log('page-title-updated')
        console.log('页面标题更新事件')
    })
    webContent.on('dom-ready', (event, args) => {
        console.log('dom-ready')
        console.log('页面dom加载完成时事件--网页的DOMContentLoaded事件')
    })
    webContent.on('did-frame-finish-load', (event, args) => {
        console.log('did-frame-finish-load')
        console.log('框架加载完成事件')
    })
    webContent.on('did-finish-load', (event, args) => {
        console.log('did-finish-load')
        console.log('当前页面加载完成事件')
    })
    webContent.on('page-favicon-updated', (event, args) => {
        console.log('page-favicon-updated')
        console.log('页面icon图标更新时事件')
    })
    webContent.on('did-stop-loading', (event, args) => {
        console.log('did-stop-loading')
        console.log('页面所有内容加载完成时事件')
    })
}

function webContentRedirectEvent(webContent) {
    webContent.on('will-redirect', (event, args) => {
        console.log('will-redirect - 服务端返回301或302,浏览器准备跳转时触发,可通过event.preventDefault()取消跳转')
    })
    webContent.on('did-redirect-navigation', (event, args) => {
        console.log('did-redirect-navigation - 服务端返回301或302,浏览器开始跳转时触发,不可取消此事件')
    })
    webContent.on('will-navigation', (event, args) => {
        console.log('will-navigation - 点击链接或者JS设置window.location.href属性,页面发生跳转前触发')
    })
    webContent.on('will-start-navigation', (event, args) => {
        console.log('will-start-navigation - 点击链接或者JS设置window.location.href属性,页面发生跳转前触发')
    })
    webContent.on('did-navigation-in-page', (event, args) => {
        console.log('did-navigation-in-page - 更新window.location.hash或用户点击锚点链接时触发')
    })
    webContent.on('did-frame-navigation', (event, args) => {
        console.log('did-frame-navigation - 主页面和子页面跳转完成时')
    })
    webContent.on('did-navigation', (event, args) => {
        console.log('did-navigation - 主页面跳转完成时')
    })
}